<?php

namespace App\Http\Controllers\API;

use App\Models\Picture;
use App\Repositories\PicturesRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

class PicturesController extends Controller
{

    protected $store;
    protected $picture;

    public function __construct(PicturesRepository $picturesRepository)
    {
        $this->picture = $picturesRepository;
    }

    public function upload(Request $request)
    {
        $user_id = getSessionField('id');
        $file = $request->file('file');
        if($file){
            //获取文件夹名称
            $directory = 'pics';
            $picture = $this->picture->uploadByHash($file,$directory,$user_id?$user_id:1,$request->all());
            if($picture){
                return responseZK(1,$picture);
            }else{
                return responseZK(0,[],'上传失败');
            }
        }else{
            return responseZK(0,[],'没有文件');
        }
    }

    public function uploadTemp(Request $request)
    {
        $user_id = getSessionField('id');
        $file = $request->file('file');
        if($file){
            //获取文件夹名称
            $directory = 'temp';
            $picture = $this->picture->uploadByHash($file,$directory,$user_id?$user_id:1,$request->all());
            if($picture){
                return responseZK(1,$picture);
            }else{
                return responseZK(0,[],'上传失败');
            }
        }else{
            return responseZK(0,[],'没有文件');
        }
    }
    public function uploadAdmin(Request $request)
    {
        $shop = Auth::guard('admin')->user()->shop;
        $user_id = getSessionField('id');
        $file = $request->file('file');
        if($file){
            //获取文件夹名称
            $directory = 'admin_'.isset($shop)?$shop->id:0;
            $picture = $this->picture->uploadByHash($file,$directory,$user_id?$user_id:1,$request->all());
            if($picture){
                return responseZK(1,$picture);
            }else{
                return responseZK(0,[],'上传失败');
            }
        }else{
            return responseZK(0,[],'没有文件');
        }
    }

}
